package cn.tedu.web.servlet;

import cn.tedu.web.JdbcUtils;
import cn.tedu.web.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 王艳林
 * @email 981247127@qq.com
 * @time 2022-06-09 14:17:35
 **/
@WebServlet(urlPatterns = "/authentication/login")
public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收前端提交的参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// 根据前端的提交的账号和密码，到数据库中查询用户信息
		String sql = "SELECT id, username, `nickname` FROM ec_simple_user WHERE username = ? AND `password` = MD5(?)";
		User user = JdbcUtils.select(sql, User.class, username, password);
		// 只要 user 和 user.id 不是 null 值，视为账号和密码匹配
		boolean isOk = user != null && user.getId() != null;
		PrintWriter writer = response.getWriter();
		if(isOk) {
			writer.write("OK");
		} else {
			writer.write("NO");
		}
		writer.flush();
	}
}